package com.changgou.file.util;

import com.changgou.file.FastDFSFile;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.springframework.core.io.ClassPathResource;


/**
 * @author 1
 * @data 2022/1/9
 */
public class FastDFSClient {

    //静态代码块中加载配置所有的方法都会加载配置所以在静态代码块中获取
    static{
        ClassPathResource classPathResource = new ClassPathResource("fdfs_client.conf");
        try {
            ClientGlobal.init(classPathResource.getPath());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //上传图片
    public static String[] upload(FastDFSFile file) throws Exception {
        //创建一个配置文件 填写服务端ip端口
        //加载配置文件建立连接
        //ClientGlobal.init("");

        //创建trackerClient
        TrackerClient trackerClient = new TrackerClient();
        //根据trackerClient获取连接对象
        TrackerServer trackerServer = trackerClient.getConnection();
        //创建storageServer 对象设置null
        //创建storageClient 提供了很多的操作图片的Api
        StorageClient storageClient = new StorageClient(trackerServer, null);

        //上传图片‪E:\Pictures\6.jpg
        //参数一 指定要上传的图片的本地绝对路径
        //参数二 指定要上传的图片的后缀名 不带点（jpg、png..）
        //参数三 指定要上传的图片的元数据 也就是 图片的高度 日期 像素等等 可以不给设置为null
        NameValuePair[] meta_list = new NameValuePair[]{
                new NameValuePair(file.getName())
        };
        String[] jpgs = storageClient.upload_file(file.getContent(), file.getExt(), meta_list);

        return jpgs;
    }

    /**
     *下载图片
     * @param groupName 组名
     * @param remoteFileName 数据库中存储的文件完整名
     * @return
     * @throws Exception
     */
    public static byte[] download(String groupName,String remoteFileName) throws Exception {
        //加载配置文件
        //ClientGlobal.init("D:\\Java\\javaEE\\changgou\\changgou-parent\\changgou-service\\changgou-service-file\\src\\main\\resources\\fdfs_client.conf");
        //创建trackerClient
        TrackerClient trackerClient = new TrackerClient();
        //根据trackerClient获取连接对象
        TrackerServer trackerServer = trackerClient.getConnection();
        //创建storageClient storageServer设置为null
        StorageClient storageClient = new StorageClient(trackerServer, null);
        //下载
        //参数一 指定要下载的组名
        //参数二 指定要下载的文件的路径
        byte[] bytes = storageClient.download_file(groupName,remoteFileName);
        return bytes;
    }


    /**
     * 删除图片
     * @param groupName 组名
     * @param remoteFileName 数据库中的文件完整名
     * @throws Exception
     */
    public static boolean deleteFile(String groupName,String remoteFileName) throws Exception {
        //读取配置文件
        //ClientGlobal.init("D:\\Java\\javaEE\\changgou\\changgou-parent\\changgou-service\\changgou-service-file\\src\\main\\resources\\fdfs_client.conf");

        //创建tracker客户端
        TrackerClient trackerClient = new TrackerClient();
        //根据trackerClient获取连接对象
        TrackerServer trackerServer = trackerClient.getConnection();
        //创建storageClient storageServers设置为null
        StorageClient storageClient = new StorageClient(trackerServer, null);
        //删除
        int i = storageClient.delete_file(groupName, remoteFileName);
        if (i==0) {
            return true;
        }else {
            return false;
        }
    }

    /**
     * 获取图片信息
     * @param groupName 组名 
     * @param remoteFileName 数据库中的图片完整名
     * @return
     * @throws Exception
     */
    public static FileInfo getFileInfo(String groupName,String remoteFileName) throws Exception {
        //读取配置文件
        //ClientGlobal.init("D:\\Java\\javaEE\\changgou\\changgou-parent\\changgou-service\\changgou-service-file\\src\\main\\resources\\fdfs_client.conf");

        //创建trackerClient客户端
        TrackerClient trackerClient = new TrackerClient();
        //获取连接
        TrackerServer trackerServer = trackerClient.getConnection();
        //创建storageClient 并是指StorageServers为空null
        StorageClient storageClient = new StorageClient(trackerServer, null);
        //调用获取信息的方法
        FileInfo group1 = storageClient.get_file_info(groupName, remoteFileName);
        //System.out.println(group1);
        return group1;
    }
}
